#pragma once
#include "resource.h"
#include "CdiWindow.h"
#include <tchar.h>

CdiWindow::CdiWindow( WNDPROC wndProc, LPCWSTR Title) 
  : m_title( Title ), m_isInit( false ), m_hWnd( NULL )
{
  Setup( wndProc );
}

CdiWindow::CdiWindow( WNDPROC wndProc, LPCWSTR Title, int screenWidth, int screenHeight ) 
  : m_title( Title ), m_isInit( false ), m_hWnd( NULL ), m_screenHeight( screenHeight ), m_screenWidth( screenWidth )
{
  Setup(wndProc);
}

CdiWindow::~CdiWindow(void)
{
  m_title = NULL;
  m_hWnd = NULL;
}

void CdiWindow::Setup( WNDPROC wndProc )
{

  ReadConfig();

  HINSTANCE hInstance = (HINSTANCE) GetModuleHandle( NULL );
  m_wndClassEx.cbSize = sizeof( WNDCLASSEX );
  m_wndClassEx.cbClsExtra = 0;
  m_wndClassEx.cbWndExtra = 0;
  m_wndClassEx.hbrBackground = CreateSolidBrush( RGB(255,255,255) ); 
  m_wndClassEx.hCursor = LoadCursor( NULL, IDC_ARROW );
  m_wndClassEx.hIcon = LoadIcon( NULL, MAKEINTRESOURCE(IDI_LOGO) );
  m_wndClassEx.hIconSm = LoadIcon( NULL, MAKEINTRESOURCE(IDI_LOGO) );
  m_wndClassEx.lpfnWndProc = wndProc;
  m_wndClassEx.lpszClassName = m_title;
  m_wndClassEx.lpszMenuName = NULL;
  m_wndClassEx.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
  m_wndClassEx.hInstance = hInstance;
    
  ATOM atom = RegisterClassEx( &m_wndClassEx );
  HRESULT dwError = GetLastError();
    
  m_hWnd = CreateWindowEx( 0, 
        m_title,        // name of window class 
        m_title,            // title-bar string 
        WS_OVERLAPPEDWINDOW, // top-level window 
        CW_USEDEFAULT,       // default horizontal position 
        CW_USEDEFAULT,       // default vertical position 
        m_screenWidth,       // default width 
        m_screenHeight,       // default height 
        (HWND) NULL,         // no owner window 
        (HMENU) NULL,        // use class menu 
        hInstance,           // handle to application instance 
        (LPVOID) NULL);      // no window-creation data

  dwError = GetLastError();

  if( NULL == m_hWnd )
  {
    OutputDebugString( _T("Unable to create window") );
    return;
  }

  m_isInit = true;
}

void CdiWindow::Start()
{
  AssertValid();

  if( false == IsInit() )
  {
    OutputDebugString( _T("Application not init") );
    return;
  }

  BOOL bUpdatedWindow = UpdateWindow( m_hWnd );
  if( FALSE == bUpdatedWindow )
    OutputDebugString( _T("Window was not updated" ) );

  bUpdatedWindow = ShowWindow( m_hWnd, SW_SHOW );
  if( FALSE == bUpdatedWindow ) 
    OutputDebugString( _T("Window could not be shown") );

  MSG msg;
  memset( &msg, 0, sizeof( MSG ) );
  while( TRUE == GetMessage(&msg, NULL, 0, 0 ) ) 
  {
    TranslateMessage( &msg );
    DispatchMessage( &msg );
  }
}

void CdiWindow::SetTitle( LPCWSTR Title )
{
  m_title = Title;
  if( NULL != m_hWnd && IsInit() )
  {
    SetWindowText( m_hWnd, Title );
  }
}

// Tells if the current window has focus
bool CdiWindow::HasFocus(void)
{
  return (m_hWnd == GetFocus());
}

// Sets the mouse position to the center of the active window
void CdiWindow::CenterMousePosition(void)
{
  if( false == HasFocus() )
  {
    return;
  }

  RECT boundingRect;
  GetWindowRect( m_hWnd, &boundingRect );

  m_screenWidthMiddle = ( boundingRect.left + boundingRect.right ) / 2;
  m_screenHeightMiddle = ( boundingRect.top + boundingRect.bottom ) / 2;
  SetCursorPos( m_screenWidthMiddle, m_screenHeightMiddle );  
}

void CdiWindow::AssertValid()
{
  _ASSERT( m_hWnd != NULL );
}

